package com.aaa.tmr.controller;

import com.aaa.tmr.entity.LayUiTree;
import com.aaa.tmr.entity.Menu;
import com.aaa.tmr.entity.Role;
import com.aaa.tmr.service.RoleService;
import com.aaa.tmr.util.Result;
import com.aaa.tmr.util.ReturnStatus;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色信息表(Role)表控制层
 *
 * @author makejava
 * @since 2021-03-24 17:49:48
 */
@Controller
@RequestMapping("role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ResponseBody
    @RequestMapping("selectOne")
    public Result selectOne(Integer id) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0L,
                this.roleService.queryById(id));
    }

    /**
     * 跳转部门分页列表页面
     *
     * @return
     */
    @RequestMapping("toRole")
    public String toPage() {
        return "back/role-page";
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page, Integer limit, Role role) {
        PageInfo pageInfo = roleService.queryAll(page, limit, role);
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("insert")
    public Result insert(Role role, String menus) {
        Role insert = roleService.insert(role);
        if (insert != null) {
            String[] menusId = menus.split(",");
            roleService.insertRoleMenu(insert.getRoleId(), menusId);
            return new Result(ReturnStatus.SUCCESS.getCode(),
                    ReturnStatus.SUCCESS.getMessage(), 0L,
                    insert);
        }
       return null;
    }

    /**
     * 修改数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("update")
    public Result update(Role role) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0L,
                this.roleService.update(role));
    }

    /**
     * 通过主键删除数据
     *
     * @param roleId 主键
     * @return 是否成功
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public Result deleteById(Integer roleId) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0L,
                this.roleService.deleteById(roleId));
    }
    @ResponseBody
    @GetMapping("findAllMenu")
    public List<LayUiTree> findAllMenu() {
        List<Menu> menus = roleService.findAllMenu();

        List<LayUiTree> list = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId() == 0) {
                LayUiTree tree = new LayUiTree();
                tree.setId(menu.getMenuId());
                tree.setTitle(menu.getMenuName());
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                //组件展开
//                tree.setSpread(true);
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                list.add(tree);
            }
        }
        return list;
    }
    public List<LayUiTree> getChildren(Integer parentId, List<Menu> menus) {
        List<LayUiTree> list = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId().equals(parentId)) {
                LayUiTree tree = new LayUiTree();
                tree.setId(menu.getMenuId());
                tree.setTitle(menu.getMenuName());
                //组件展开
//                tree.setSpread(true);
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                list.add(tree);
            }
        }
        return list;
    }
}
